# cython: boundscheck=False
# cython: wraparound=False
from cython cimport boundscheck,wraparound

from libc.stdlib cimport malloc,free


def summer1(double[:] mv):
    cdef:
        double d,ss=0.0
    for d in mv:
        ss+=d
    return ss

###比较快
def summer2(double[:] mv):
    cdef:
        double d,ss=0.0
        int i,N
    N=mv.shape[0]
    for i in range(N):
        ss+=mv[i]
    return ss

###更快
##取消了越界检查和负索引
def summer(double[:] mv):
    cdef:
        double d,ss=0.0
        int i,N
    N=mv.shape[0]
    for i in range(N):
        ss+=mv[i]
    return ss


cdef struct coorlist:
    int x
    int y
    double val
    void *next

cdef class Particle:
    """Simple Particle extension type."""
    cdef public double position, velocity,mass

cdef class Matrix:
    cdef public unsigned int nrows, ncols
    cdef double *_matrix       #指针不能与python互通，所以不能使用public
    def __cinit__(self, nr, nc):
        self.nrows = nr
        self.ncols = nc
        self._matrix = <double*>malloc(nr * nc * sizeof(double))
        if self._matrix == NULL:
            raise MemoryError()
    def __dealloc__(self):
        if self._matrix != NULL:
            free(self._matrix)

def class_test():
    cdef Particle pt
    cdef Matrix mt
    pt=Particle(2,3,3)
    mt=Matrix(2,3)
    return pt,mt


def list_test(double[:,:] image):
    cdef:
        int height,width,i,j,list_count
        coorlist **row_l_s
        coorlist **row_l_e
        coorlist *list
        coorlist *start
    height,width = image.shape[:2]
    print("%d,%d"%(height,width))
    list=<coorlist*> malloc(height*width*sizeof(coorlist))
    row_l_s=<coorlist**> malloc(height*sizeof(coorlist*))
    row_l_e=<coorlist**> malloc(height*sizeof(coorlist*))

    for i in range(height):
        row_l_e[i]=NULL
        row_l_s[i]=NULL

    list_count=0
    for i in range(height):
        for j in range(width):
             #从链表取一个元素,放到对应bin
            if row_l_s[i]==NULL:
                row_l_s[i]=&list[list_count]    ##当前bin为空时
                row_l_e[i]=row_l_s[i]
            elif row_l_s[i]==row_l_e[i]:        ##当前bin有一个元素
                row_l_e[i]=&list[list_count]
                row_l_s[i].next=<void*>&list[list_count]
            else:                               ##当前bin有多于一个元素时
                row_l_e[i].next=<void*>&list[list_count]
                row_l_e[i]=&list[list_count]
            list_count=list_count+1
            #print(list_count)
            row_l_e[i].x=j
            row_l_e[i].y=i
            row_l_e[i].val=image[i,j]
            row_l_e[i].next=NULL
    print("ok")
    ##打印出来
    for i in range(height-1,-1,-1):
        print("bin=%d"%i)
        start=row_l_s[i]
        while start.next!=NULL:
            start=<coorlist *>start.next
        print()


"""
    for i in range(height):
        row_l_e[i]=NULL
        row_l_s[i]=NULL

    list_count=0
    row_l_s[0]=&list[0]

    row_l_s[0].next=<void*>row_l_e[i]
    row_l_e[0]=&list[1]
    row_l_s[0].val=10
    print(row_l_s[0].val)
"""


"""
    list_count=0
    row_l_s[0]=&list[list_count]
    row_l_e[0]=row_l_s[list_count]

    row_l_e[0].next=<void*>&list[list_count]
    row_l_e[0]=&list[list_count]
    list_count=list_count+1
    row_l_e[0].x=1
    row_l_e[0].y=2
    row_l_e[0].val=3
    row_l_e[0].next=NULL

    print(row_l_e[0].val)
"""